<?php
/**
 * 部门管理
 * Enter description here ...
 * @author cc
 *
 */
namespace Home\Controller;
use Think\Controller;
class DeptController extends CommonController{
public function index(){
		$this->display();
	}
	
	public function nav(){
		$dept = M("sys_dept");
		$data1 = $dept->where('dept_level=0')->order('dept_no asc')->select();//查询一级部门
		if(!empty($data1)){
			foreach ($data1 as $key1=>$value1){
				$dept_id1 = $value1['id'];
				$data2 = $dept->where('dept_level='.$dept_id1)->order('dept_no asc')->select();//查询二级部门
				if(!empty($data2)){
					foreach ($data2 as $key2=>$value2){
						$dept_id2 = $value2['id'];
						$data3 = $dept->where('dept_level='.$dept_id2)->order('dept_no asc')->select();//查询三级部门
						if(!empty($data3)){
							foreach ($data3 as $key3=>$value3){
								$dept_id3 = $value3['id'];
								$data4 = $dept->where('dept_level='.$dept_id3)->order('dept_no asc')->select();//查询四级部门
								if(!empty($data4)){
									foreach ($data4 as $key4=>$value4){
										$dept_id4 = $value4['id'];
										$data5 = $dept->where('dept_level='.$dept_id4)->order('dept_no asc')->select();//查询五级部门
										if(!empty($data5)){
											foreach ($data5 as $key5=>$value5){//如果级别很多的话，就继续写
//												$dept_id5 = $value5['id'];
//												$data6 = $dept->where('dept_level='.$dept_id5)->order('dept_no asc')->select();//查询六级部门
//												if(!empty($data6)){
//													
//												}
												$data[$key1]['one'][$key2]['two'][$key3]['three'][$key4]['four'][$key5]['dept_id'] = $value5['id'];
												$data[$key1]['one'][$key2]['two'][$key3]['three'][$key4]['four'][$key5]['dept_name'] = $value5['dept_name'];
											}
										}
										$data[$key1]['one'][$key2]['two'][$key3]['three'][$key4]['dept_id'] = $value4['id'];
										$data[$key1]['one'][$key2]['two'][$key3]['three'][$key4]['dept_name'] = $value4['dept_name'];
									}
								}
								$data[$key1]['one'][$key2]['two'][$key3]['dept_id'] = $value3['id'];
								$data[$key1]['one'][$key2]['two'][$key3]['dept_name'] = $value3['dept_name'];
							}
						}
						$data[$key1]['one'][$key2]['dept_id'] = $value2['id'];
						$data[$key1]['one'][$key2]['dept_name'] = $value2['dept_name'];
					}
				}
				$data[$key1]['dept_id'] = $value1['id'];
				$data[$key1]['dept_name'] = $value1['dept_name'];
			}
			
		}
		
		$this->assign('data',$data);
		$this->display();
	}
	
	public function add(){
		$dept = M("sys_dept");
		$data_dept = $dept->select();
		$this->assign('data_dept',$data_dept);
		$this->display();
	}
	
	public function insert(){
		if(!IS_POST){ $this->error("非法表单");exit;}
		$data = $_POST;
		$dept = M("sys_dept");
		$dept_no = $_POST['dept_no'];
		$dept_level = $_POST['dept_level'];
		$dept_name = I('post.dept_name');
		$data_dept = $dept->where('dept_no='.$dept_no.' and dept_level='.$dept_level)->find();
		if(!empty($data_dept)){
			$this->error("对不起，同级部门下该编号已被占用！");
		}
		$data_dept1 = $dept->where("dept_name='%s'",$dept_name)->find();
		if(!empty($data_dept1)){
			$this->error("对不起，该部门名称已存在！");
		}
		$success = $dept->add($data);
		if($success){
			$this->systemlog('add', '部门管理', session('name').'新建部门--'.$_POST['dept_name']);
			$this->success('新建部门成功！','add');
		}else {
			$this->error('新建部门失败！');
		}
	}
	
	public function user_select(){//选择负责人
		$type = I('get.type');
		$user = M("sys_user");
		$data_user = $user->where("status=1")->order('user_name')->select();
		$this->assign('data_user',$data_user);
		$this->assign('type',$type);
		$this->display();
	}
	
	public function edit(){
		$dept_id = I('get.dept_id');
		$dept = M("sys_dept");
		$data_dept = $dept->select();
		$data = $dept->where("id=%d",$dept_id)->find();
		$this->assign('data_dept',$data_dept);
		$this->assign("data", $data);
		$this->assign('id',$dept_id);
		$this->display();
	}
	
	Public function update(){
		if(!IS_POST){ $this->error("非法表单");exit;}
		$data = $_POST;
		$id = I('post.id');
		$dept = M("sys_dept");
		$dept_no = $_POST['dept_no'];
		$dept_level = $_POST['dept_level'];
		$dept_name = I('post.dept_name');
		$data_dept = $dept->where('id != '.$id.' and dept_no='.$dept_no.' and dept_level='.$dept_level)->find();
		if(!empty($data_dept)){
			$this->error("对不起，同级部门下该编号已被占用！");
		}
		$data_dept1 = $dept->where("id !=%d and dept_name='%s'",$id,$dept_name)->find();
		if(!empty($data_dept1)){
			$this->error("对不起，该部门名称已存在！");
		}
		$success = $dept->where("id=%d", $id)->data($data)->save();
		if($success){
			$this->systemlog('edit', '部门管理', session('name').'修改部门--'.$_POST['dept_name']);
			$this->success('修改部门成功！','edit?dept_id='.$id);
		}else {
			$this->error('修改部门失败！');
		}
	}
	
	public function delete(){
		$dept_id = I('get.dept_id');
		$dept = M("sys_dept");
		$dept_name = $dept->where('id=$d',$dept_id)->getField("dept_name");
		$success = $dept->where('id=$d',$dept_id)->delete();
		if($success){
			$this->systemlog('delete', '部门管理', session('name').'删除部门--'.$dept_name);
			$this->success('删除部门成功！','add');
		}else {
			$this->error('删除部门失败！');
		}
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>